package orderCus;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.Customer;
import common.Item;
/**
 * サイドメニューの商品コード検索で入力された情報を受け取り、商品またはトップに遷移する
 * @author matsumoto
 *
 */
public class ItemInfoServlet extends HttpServlet 
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	private void perform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		//ログインチェック
		HttpSession session = req.getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(user == null || user.equals("null")) resp.sendRedirect(req.getContextPath() +"/index.jsp");
		else
		{
			req.setCharacterEncoding("UTF-8");
			String code = req.getParameter("code");
			String msg = null;
			
			if(code != null && code.length() != 0){
				Item item = Item.find_by_code(code);
				if(item != null ){
					req.setAttribute("item", item);
				}
				else{
					msg = "該当する商品がありません。";
				}
			}
			else{
				msg = "検索項目を選択または入力してください。";
			}
			
			String url = "/customer/top.jsp";
			if(msg == null || msg.length() == 0){
				url = "/customer/itemInfo.jsp";
			}
			req.setAttribute("msg", msg);
			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, resp);
		}
	}
}
